#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/1 15:56
# @Author  : huidong.bai
# @File    : XMLReporter.py
# @Software: PyCharm
# @Mail    : MasterBai2018@outlook.com

import os
import json
import pdb
import time
from typing import Optional
from src.core.Common.Bean.Conveyor import Conveyor


class MetaErrorInfo:
    def __init__(self, input, expect, actual):
        self.input = input
        self.expect = expect
        self.actual = actual


class MetaTestCaseResult:
    def __init__(self):
        self.caseName = None
        self.bugGrade = None
        self.errInfo = []


class TestSuite:
    def __init__(self):
        self.suiteName = None
        self.suiteConfig = None
        self.suiteCaseList = None
        self.suiteID = None
        self.suiteTag = None
        self.suiteAbstract = None
        self.summary = None
        self.caseInfo = []
        self.skip_caseInfo = []


class XMLReporter:
    def __init__(self):
        self.m_conveyor = None
        self.report_path = None
        self.test_suite = None
        self.case_infos = []        
        self.skip_case_infos = []
        self.push_flag = False
        self.start_time = None
        self.end_time = None

    def set_param(self, conveyor: Conveyor):
        self.m_conveyor = conveyor

    def push_start(self, suite_name: str):
        if self.push_flag:
            return
        self.push_flag = True
        self.start_time = time.time()

        report_name = self.m_conveyor.report.split(":")[1]
        self.report_path = os.path.join(self.m_conveyor.workspace, report_name)

        suite = TestSuite()
        suite.suiteName = suite_name
        suite.suiteConfig = self.m_conveyor.config
        suite.suiteCaseList = self.m_conveyor.case_list
        suite.suiteID = int(self.m_conveyor.suite_id)
        suite.suiteTag = self.m_conveyor.suite_tag
        suite.suiteAbstract = self.m_conveyor.suite_abstract
        self.test_suite = suite

    def push_result(self, case_name: str, err_info: MetaErrorInfo,  bug_grade: Optional[str] = None):
        if not self.push_flag:
            return
        self.push_flag = True
        self.end_time = time.time()

        case_result = MetaTestCaseResult()
        case_result.caseName = case_name
        case_result.bugGrade = bug_grade
        case_result.errInfo.append(err_info.__dict__)
        self.case_infos.append(case_result.__dict__)
        self.generate_report()

    def push_stop(self):
        if not self.push_flag:
            return
        self.end_time = time.time()
        self.generate_report()
        self.push_flag = False

    def generate_report(self):
        self.json_report()

    def json_report(self):
        total_time = int(self.end_time - self.start_time)
        failed_num = len(self.case_infos)
        skiped_num = len(self.skip_case_infos)
        passed = "yes" if failed_num <= 0 and skiped_num <= 0 else "no"
        summary = {"totalTime": total_time,
                   "caseNum": self.m_conveyor.case_nums,
                   "failedNum": failed_num,
                   "skipedNum": skiped_num,
                   "S": 0,
                   "A": failed_num,
                   "B": 0,
                   "C": 0,
                   "passed": passed}
        self.test_suite.summary = summary
        self.test_suite.caseInfo = self.case_infos
        self.test_suite.skip_caseInfo = self.skip_case_infos

        with open(self.report_path, "w", encoding="utf-8") as fp:
            json.dump(self.test_suite.__dict__, fp, indent=2, ensure_ascii=False)
            fp.close()
